package com.wavehi.consumer;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.wavehi.util.RabbitMQFactoryUtils;
import com.wavehi.util.SleepUtils;

/**
 * @Description 消费者 演示预取值分发
 * @Author WaveHi
 * @Date 26/12/2021 下午 7:24
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 */
public class Consumer_07 {

    //队列名
    private static final String QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {

        //1.获取连接信道
        Channel channel = RabbitMQFactoryUtils.getChannel();
        //1.1.设置预取值
        channel.basicQos(5);
        //2.接收消息
        //2.1.准备消费操作
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            SleepUtils.sleep(20);
            System.out.println(new String(message.getBody(), "UTF-8"));
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };
        //2.2.准备消费中断后的操作
        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("Send message stop!");
        };
        //3.3.接收
        channel.basicConsume(QUEUE_NAME, false, deliverCallback, cancelCallback);
    }
}
